layui.use(['grid', 'form'], function () {
    var grid = layui.grid,
        form = layui.form,
        options = {
            tableId: 'menu-table',
            tableFilter: 'menu-table-filter',
            table: {
                url: Feng.ctxPath + "/system/menu-list",
                method: 'post',
                cols: [[
                    {type: 'numbers'},
                    {type: "checkbox"},
                    {"hide": true, "field": "id", "title": "记录ID"},
                    {"sort": true, "field": "title", "title": "名称"},
                    {"edit": "text", "sort": true, "field": "href", "title": "链接"},
                    {width: 100, align: 'center', field: "pos", title: "位置"},
                    {"field": "pid", "title": "父菜单"},
                    {width: 60, align: 'center', field: "icon", title: "图标", templet: "#iconTpl"},
                    {width: 80, align: 'center', edit: "text", sort: true, field: "sort", title: "排序"},
                    {width: 100, align: 'center', field: "active", title: "激活", templet: "#activeTpl"},
                    {"width": 170, "toolbar": "#tool", "title": "操作"}
                ]]
            },
            eventOptions: {
                create: {
                    url: "/system/menu-form"
                },
                deleteBatch: {
                    url: "/delete-batch",
                    ajax: function (config, ids, index) {
                    }
                },
                update: {
                    url: "/update",
                    condition: function (data) {
                        return {id: 456}
                    }
                }
            }
        },
        events = {
            abc: function (obj, config, that) {
                console.log(obj)
                console.log(config)
            }
        };

    grid.render(options, events);

    /*form.on('switch(switch-filter)', function (data) {
        console.log(data.elem); //得到checkbox原始DOM对象
        console.log(data.elem.checked); //开关是否开启，true或者false
        console.log(data.value); //开关value值，也可以通过data.elem.value得到
        console.log(data.othis); //得到美化后的DOM对象
    });*/
});

// layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'treeTable', 'func', 'jquery'], function () {
/*var layer = layui.layer;
var form = layui.form;
var $ZTree = layui.ztree;
var $ax = layui.ax;
var laydate = layui.laydate;
var admin = layui.admin;
var treeTable = layui.treeTable;
var func = layui.func;

//table的初始化实例
var insTb;

/!**
 * 系统管理--菜单管理
 *!/
var Menu = {
    tableId: "menu-table",    //表格id
    toolbar: "#toolBar",
    condition: {
        menuId: "",
        menuName: "",
        level: ""
    }
};

/!**
 * 初始化表格的列
 *!/
Menu.initColumn = function () {
    return [
        {type: 'numbers'},
        // {"fixed": "left", "type": "checkbox"},
        {"width": 60, "field": "_serial", "title": "#"},
        {"hide": true, "field": "id", "title": "记录ID"},
        {"edit": "text", "sort": true, "field": "title", "title": "名称"},
        {"edit": "text", "sort": true, "field": "href", "title": "链接"},
        {"width": 100, "field": "pos", "title": "位置"},
        {"field": "pid", "title": "父菜单"},
        {"width": 60, "field": "icon", "title": "图标"},
        {"edit": "text", "sort": true, "field": "sort", "title": "排序"},
        {"edit": "radio", "event": "active", "field": "active", "title": "激活"},
        {"width": 170, "toolbar": "#tool", "title": "操作"}
        // {align: 'center', toolbar: '#menuTableBar', title: '操作', minWidth: 200}
    ];
};

/!**
 * 点击菜单树时
 *!/
Menu.onClickMenu = function (e, treeId, treeNode) {
    Menu.condition.menuId = treeNode.id;
    Menu.search();
};

/!**
 * 点击查询按钮
 *!/
Menu.search = function () {
    var queryData = {};
    queryData['menuName'] = $("#menuName").val();
    queryData['level'] = $("#level").val();
    Menu.initTable(Menu.tableId, queryData);
};

/!**
 * 弹出添加菜单对话框
 *!/
Menu.openAddMenu = function () {
    func.open({
        height: 720,
        title: '添加菜单',
        content: Feng.ctxPath + '/menu/menu_add',
        tableId: Menu.tableId,
        endCallback: function () {
            Menu.initTable(Menu.tableId);
        }
    });
};

/!**
 * 点击编辑菜单按钮时
 *
 * @param data 点击按钮时候的行数据
 *!/
Menu.onEditMenu = function (data) {
    func.open({
        height: 720,
        title: '修改菜单',
        content: Feng.ctxPath + "/menu/menu_edit?menuId=" + data.menuId,
        tableId: Menu.tableId,
        endCallback: function () {
            Menu.initTable(Menu.tableId);
        }
    });
};

/!**
 * 点击删除菜单按钮
 *
 * @param data 点击按钮时候的行数据
 *!/
Menu.onDeleteMenu = function (data) {
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/menu/remove", function () {
            Feng.success("删除成功!");
            Menu.condition.menuId = "";
            Menu.initTable(Menu.tableId);
        }, function (xhr) {
            Feng.error("删除失败!" + xhr.responseJSON.message + "!");
        });
        ajax.set("menuId", data.menuId);
        ajax.start();
    };
    Feng.confirm("是否删除菜单" + data.name + "?", operation);
};

/!**
 * 初始化表格
 *!/
Menu.initTable = function (menuId, reqData) {
    return treeTable.render({
        elem: '#' + menuId,
        toolbar: "#toolBar",
        tree: {
            iconIndex: 1,           // 折叠图标显示在第几列
            idName: 'id',         // 自定义id字段的名称
            pidName: 'parentId',       // 自定义标识是否还有子节点的字段名称
            // haveChildName: 'parentId',  // 自定义标识是否还有子节点的字段名称
            isPidData: true         // 是否是pid形式数据
        },
        height: "full-98",
        cols: Menu.initColumn(),
        reqData: function (data, callback) {
            var ajax = new $ax(Feng.ctxPath + '/system/menu-list', function (res) {
                callback(res.data);
            }, function (res) {
                Feng.error("删除失败!" + res.message + "!");
            });
            ajax.setData(reqData);
            ajax.start();
        }
    });
};

// 渲染表格
insTb = Menu.initTable(Menu.tableId);
$('#expandAll').click(function () {
    insTb.expandAll();
});
$('#foldAll').click(function () {
    insTb.foldAll();
});

//渲染时间选择框
laydate.render({
    elem: '#timeLimit',
    range: true,
    max: Feng.currentDate()
});

//初始化左侧部门树
var ztree = new $ZTree("menuTree", "/system/menu-list/select");
ztree.bindOnClick(Menu.onClickMenu);
ztree.init();

// 搜索按钮点击事件
$('#btnSearch').click(function () {
    Menu.search();
});

// 添加按钮点击事件
$('#btnAdd').click(function () {
    Menu.openAddMenu();
});

// 工具条点击事件
treeTable.on('tool(menuTable)', function (obj) {
    var data = obj.data;
    var layEvent = obj.event;

    if (layEvent === 'edit') {
        Menu.onEditMenu(data);
    } else if (layEvent === 'delete') {
        Menu.onDeleteMenu(data);
    }
});*/

// });
